/**
 * TDS PEC 4
 * Grupo 3H2M
 *
 * El paquete <code>edu.uoc.tds.pec4.mantenimiento</code> agrupa todas las
 * interfaces, clases y elementos de la GUI para la gestión del subsistema 
 * de Mantenimiento
 */
package edu.uoc.tds.pec4.mantenimiento;

import edu.uoc.tds.pec4.rmi.GestorMantenimientoInterface;
import edu.uoc.tds.pec4.beans.AulaDTO;
import edu.uoc.tds.pec4.beans.UsuarioDTO;
import edu.uoc.tds.pec4.common.util.TDSLanguageUtils;
import edu.uoc.tds.pec4.common.util.UtilidadesGUI;
import edu.uoc.tds.pec4.server.bbdd.exceptions.GestorMantenimientoException;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;

/**
 * La clase <code>DialogBuscaAula</code> representa la interfaz de
 * usuario para la búsqueda de un aula.
 * Permitirá la localización de un aula por su identificador, 
 * o su nombre.<br />
 *
 * Interficie de usuario para la búsqueda de aulas.
 *
 * @author	Luis Rivas Mariño
 * @version	15/12/2011
 * @see		edu.uoc.tds.pec4.rmi.GestorMantenimientoInterface
 */
public class DialogBuscaAula extends javax.swing.JDialog {

    /** clientConnected Indica si el cliente está conectado al servidor */
    private boolean clientConnected = false;
    /** objRemoto Es el objeto empleado como interfaz en la comunicación RMI */
    private GestorMantenimientoInterface objRemotoMantenimiento = null;
    /** usLogeado representa al usuario que está autentificado */
    private UsuarioDTO usLogeado;

    /** Creates new form DialogBuscaAula */
    public DialogBuscaAula(java.awt.Frame parent, boolean modal, UsuarioDTO usLogeado) {
        super(parent, TDSLanguageUtils.getMessage("mantenimiento.aula.busca.titulo"), modal);

        this.usLogeado = usLogeado;

        conectarCliente();

        initComponents();

        habilitaControles();

        //Centramos la ventana en la pantalla
        UtilidadesGUI.centrarVentana(this);
    }

    /**
     * Conectar con el servidor RMI
     * Permite la conexion con el servidor RMI
     */
    private void conectarCliente() {
        try {
            objRemotoMantenimiento = (GestorMantenimientoInterface) Naming.lookup("GestorMantenimiento");
            clientConnected = true;
        } catch (NotBoundException eNbe) {
            objRemotoMantenimiento = null;
            JOptionPane.showMessageDialog(null,
                    TDSLanguageUtils.getMessage("cliente.error.notbound") + "\n" + eNbe.getMessage(),
                    TDSLanguageUtils.getMessage("cliente.error"), JOptionPane.ERROR_MESSAGE);
        } catch (RemoteException eRe) {
            objRemotoMantenimiento = null;
            JOptionPane.showMessageDialog(null,
                    TDSLanguageUtils.getMessage("cliente.error.url") + "\n" + eRe.getMessage(),
                    TDSLanguageUtils.getMessage("cliente.error"), JOptionPane.ERROR_MESSAGE);
        } catch (MalformedURLException eMULRe) {
            objRemotoMantenimiento = null;
            JOptionPane.showMessageDialog(null,
                    TDSLanguageUtils.getMessage("cliente.error.conn") + "\n" + eMULRe.getMessage(),
                    TDSLanguageUtils.getMessage("cliente.error"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eE) {
            objRemotoMantenimiento = null;
            JOptionPane.showMessageDialog(null,
                    TDSLanguageUtils.getMessage("cliente.error.conn") + "\n" + eE.getMessage(),
                    TDSLanguageUtils.getMessage("cliente.error"), JOptionPane.ERROR_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        lblIdAula = new javax.swing.JLabel();
        nombre = new javax.swing.JTextField();
        lblNombre = new javax.swing.JLabel();
        idAula = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btnBuscar = new javax.swing.JButton();
        btnVolver = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Búsqueda de aula", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        lblIdAula.setText(TDSLanguageUtils.getMessage("mantenimiento.gui.etiqueta.idaula"));

        nombre.setFont(new java.awt.Font("Tahoma", 1, 11));
        nombre.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        nombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nombreKeyReleased(evt);
            }
        });

        lblNombre.setText(TDSLanguageUtils.getMessage("mantenimiento.gui.etiqueta.nombre"));

        idAula.setFont(new java.awt.Font("Tahoma", 1, 11));
        idAula.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        idAula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                idAulaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblIdAula)
                        .addGap(18, 18, 18)
                        .addComponent(idAula, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblNombre)
                        .addGap(21, 21, 21)
                        .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIdAula)
                    .addComponent(idAula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnBuscar.setText(TDSLanguageUtils.getMessage("mantenimiento.gui.boton.buscar"));
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnVolver.setText(TDSLanguageUtils.getMessage("mantenimiento.gui.boton.volver"));
        btnVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE)
                .addComponent(btnVolver))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(btnVolver))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nombreKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nombreKeyReleased
        if (!(nombre.getText().equals(""))) {
            idAula.setEditable(false);
            idAula.setEnabled(false);
        } else if ((nombre.getText().equals(""))) {
            idAula.setEditable(true);
            idAula.setEnabled(true);
        }
    }//GEN-LAST:event_nombreKeyReleased

    private void idAulaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idAulaKeyReleased
        if (!(idAula.getText().equals(""))) {
            nombre.setEditable(false);
            nombre.setEnabled(false);
        } else if ((nombre.getText().equals(""))) {
            nombre.setEditable(true);
            nombre.setEnabled(true);
        }
    }//GEN-LAST:event_idAulaKeyReleased

    private void btnVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolverActionPerformed
        dispose();
    }//GEN-LAST:event_btnVolverActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        AulaDTO aula = null;
        try {
            if (!(idAula.getText().equals(""))) {
                aula = objRemotoMantenimiento.consultaAula(Integer.parseInt(idAula.getText()));
            } else if (!(nombre.getText().equals(""))) {
                aula = objRemotoMantenimiento.consultaAulaNombre(nombre.getText());
            }

            if (aula == null || aula.getIdAula() == null) {
                JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("mantenimiento.aula.err.buscar"));
            } else {
                Frame frame = JOptionPane.getFrameForComponent(this);
                DialogConsultaAula dlgConsultaAula = new DialogConsultaAula(frame, objRemotoMantenimiento,
                        aula, true, usLogeado);
                dlgConsultaAula.setVisible(true);
                dispose();
            }

        } catch (RemoteException eRe) {
            JOptionPane.showMessageDialog(null,
                    eRe.getMessage(), TDSLanguageUtils.getMessage("gmantenimiento.err.aula.buscar"),
                    JOptionPane.ERROR_MESSAGE);
        } catch (GestorMantenimientoException gmE) {
            JOptionPane.showMessageDialog(null,
                    gmE.getMessage(), TDSLanguageUtils.getMessage("gmantenimiento.err.aula.buscar"),
                    JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnBuscarActionPerformed

    /**
     * Habilita los controles del formulario
     */
    private void habilitaControles() {
        idAula.setEditable(true);
        idAula.setEnabled(true);
        nombre.setEditable(true);
        nombre.setEnabled(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnVolver;
    private javax.swing.JTextField idAula;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblIdAula;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JTextField nombre;
    // End of variables declaration//GEN-END:variables
}
